package org.example.admin.aircraft;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

/**
 * 更新失败
 */
public class updateAircraftInfoView extends JFrame {
    private JTextField aircraftIdField;
    private JTextField aircraftModelField ;
    private JTextField seatsField;
    private JButton updateButton;

    public updateAircraftInfoView() {
        setTitle("管理端飞机信息修改页面");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(7, 2));

        JLabel aircraftIdLabel = new JLabel("飞机id");
        aircraftIdField = new JTextField();
        JLabel aircraftModelLabel = new JLabel("飞机型号");
        aircraftModelField = new JPasswordField();
        JLabel seatsLabel = new JLabel("飞机座位");
        seatsField = new JTextField();
        updateButton = new JButton("更新");

        // 添加组件到窗口
        add(aircraftIdLabel);
        add(aircraftIdField);

        add(aircraftModelLabel);
        add(aircraftModelField);

        add(seatsLabel);
        add(seatsField);

        add(new JLabel()); // 占位
        add(updateButton);

        // 添加更新按钮的点击事件监听器
        updateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                updateUserInfo();
            }
        });
    }

    private void updateUserInfo() {
        int aircraftId = Integer.parseInt(aircraftIdField.getText());
        String aircraftModel=aircraftModelField.getText();
        String seats=seatsField.getText();

        try {
            // 连接数据库
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:Mysql://localhost:3306/airlines", "root", "123456");

            // 创建更新语句
            String updateQuery = "UPDATE air_aircraft SET model=?,seats = ? WHERE aircraft_id = ?";
            PreparedStatement statement = conn.prepareStatement(updateQuery);

            statement.setInt(1, aircraftId);
            statement.setString(2, aircraftModel);
            statement.setString(3, seats);

            // 执行更新操作
            int rowsUpdated = statement.executeUpdate();

            // 关闭连接和语句
            statement.close();
            conn.close();

            if (rowsUpdated > 0) {
                JOptionPane.showMessageDialog(this, "更新成功");
            } else {
                JOptionPane.showMessageDialog(this, "用户不存在或更新失败");
            }

            // 清空输入框
            clearFields();

        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, "更新失败：" + e.getMessage());
        }
    }

    private void clearFields() {
        aircraftIdField.setText("");
        aircraftModelField.setText("");
        seatsField.setText("");
    }

    public static void main(String[] args) {
//        SwingUtilities.invokeLater(() -> {
//            updateAircraftInfoView updateAircraftInfoForm = new updateAircraftInfoView();
//            updateAircraftInfoForm.setSize(300, 150);
//            updateAircraftInfoForm.setVisible(true);
//        });
    }
}